package org.iabako.server.serviceimpl.business;

import org.iabako.server.dao.JpaDAO;
import org.iabako.server.dao.business.InstallmentDAO;
import org.iabako.server.dao.business.PaymentRequestDAO;
import org.iabako.shared.entity.business.PaymentRequestConfig;
import org.iabako.shared.entity.user.Enterprise;
import org.iabako.shared.exception.IabakoActionForbiddenException;
import org.iabako.shared.iservice.business.PaymentRequestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * Created by jose on 29/01/15.
 */

@org.springframework.stereotype.Service("paymentRequestService")
public class PaymentRequestServiceImpl extends AbstractServiceImpl implements PaymentRequestService {

    @Autowired
    private PaymentRequestDAO paymentRequestDAO;

    @Autowired
    private InstallmentDAO installmentDAO;

    @Override
    protected JpaDAO getDAO() {
        return paymentRequestDAO;
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public PaymentRequestConfig save(PaymentRequestConfig paymentRequestConfig) throws IabakoActionForbiddenException {
        Enterprise enterprise = getEnterpriseFromSessionUser();

        if (enterprise != null && enterprise.isDemo()) {
            throw new IabakoActionForbiddenException("action_forbidden_for_demo_title", "action_forbidden_for_demo_text", true);
        }

        if (paymentRequestConfig.getEnterprise() == null) {
            paymentRequestConfig.setEnterprise(enterprise);
        }

        for (Enterprise e : enterprise.getAllRelatedEnterprises()) {
            if (!e.equals(enterprise)) {
                saveChangesInRelatedEnterprises(paymentRequestConfig, e);
            }
        }

        return (PaymentRequestConfig) super.save(paymentRequestConfig);
    }

    private void saveChangesInRelatedEnterprises(PaymentRequestConfig paymentRequestConfig, Enterprise relatedEnterprise) {
        PaymentRequestConfig relatedConfig = paymentRequestDAO.getPaymentRequestConfigByEnterprise(relatedEnterprise);
        if (relatedConfig == null) {
            //This is the first time user activates payment request
            relatedConfig = new PaymentRequestConfig();
        }
        relatedConfig.setDaysBeforePayment(paymentRequestConfig.getDaysBeforePayment());
        relatedConfig.setDaysAfterPayment(paymentRequestConfig.getDaysAfterPayment());
        relatedConfig.setEmailBeforePaymentBody(paymentRequestConfig.getEmailBeforePaymentBody());
        relatedConfig.setEmailBeforePaymentFooter(paymentRequestConfig.getEmailBeforePaymentFooter());
        relatedConfig.setEmailAfterPaymentBody(paymentRequestConfig.getEmailAfterPaymentBody());
        relatedConfig.setEmailAfterPaymentFooter(paymentRequestConfig.getEmailAfterPaymentFooter());

        super.save(relatedConfig);
    }

    public PaymentRequestConfig getByEnterprise() {

        PaymentRequestConfig paymentRequestConfig = paymentRequestDAO.getPaymentRequestConfig();

        return paymentRequestConfig != null ? paymentRequestConfig : new PaymentRequestConfig();
    }

    public List<Integer> getNumberOfInstallmentsForRequestBetween(PaymentRequestConfig paymentRequestConfig) {

        int numberOfInstallmentBefore = installmentDAO.getNumberOfInstallmentsForRequest(paymentRequestConfig, true);
        int numberOfInstallmentAfter = installmentDAO.getNumberOfInstallmentsForRequest(paymentRequestConfig, false);

        return Arrays.asList(numberOfInstallmentBefore, numberOfInstallmentAfter);
    }

    public void copyPaymentRequestConfig(Enterprise parentEnterprise, Enterprise newEnterprise) {
        PaymentRequestConfig parentConfig = paymentRequestDAO.getPaymentRequestConfigByEnterprise(parentEnterprise);
        if (parentConfig == null) {
            return;
        }
        saveChangesInRelatedEnterprises(parentConfig, newEnterprise);
    }


}
